package com.gopay.bis.citic.fund.operation;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.FundResult;
import com.gopay.common.constants.bank.CiticConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.citic.ebank.query.res.StreamRes;
import com.gopay.common.domain.bank.citic.ebank.query.res.data.CiticB2bStreamDataRes;
import com.gopay.common.domain.fund.vo.PageData;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpTransport;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * 国付宝通知监管行
 * @CiticFundQuery.java
 * @author dong-wang5@hnari.com
 * @2016年7月20日 上午10:00:54  www.gopay.com.cn Inc.All rights reserved.
 */
public class CiticFundQuery {
	private static final Logger logger = Logger.getLogger(CiticFundQuery.class);

	private CiticFundCertHelper citicFundCerthelper;
	private HttpTransport httpTransportFund;

	public FundResult send(PageData pageData)
			throws GopayException {
		logger.info("基金支付类通知发送监管行开始！！！！！！！！！！！");
		try {
			String signType = pageData.getString("sign_type");
			//刨除不参与签名数据
			pageData.remove("sign");
			pageData.remove("NOTIFY_TYPE");
			pageData.remove("CITIC");
			pageData.remove("sign_type");
			// 对数据进行签名
			String signMessage = citicFundCerthelper.sign(pageData);
			pageData.put("sign", signMessage);
			pageData.put("sign_type", signType);
			logger.info("基金支付类参数："+pageData.toString());
			String result = httpTransportFund.submit(pageData);
			logger.info("发送监管行结果："+result);
			FundResult fundResult = new FundResult();
			String[] split = result.split("&");
			for (String s : split) {
				String[] param = s.split("=");
				if (param.length>1){
					if ("is_succ".equals(param[0])){
						fundResult.setIsSuccess(param[1]);
					}else if ("serial_no".equals(param[0])){
						fundResult.setSerialNo(param[1]);
					}else if ("deposit_time".equals(param[0])){
						fundResult.setDepositTime(param[1]);
					}else if ("error_code".equals(param[0])){
						fundResult.setErrorCode(param[1]);
					}else if ("error_msg".equals(param[0])){
						fundResult.setErrorMsg(param[1]);
					}
				}
			}
			logger.info("返回结果集："+ ToStringBuilder.reflectionToString(fundResult));
			return fundResult;
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E8521,"基金支付类通知银行通信异常");
		}
	}

	public void setCiticFundCerthelper(CiticFundCertHelper citicFundCerthelper) {
		this.citicFundCerthelper = citicFundCerthelper;
	}

	public void setHttpTransportFund(HttpTransport httpTransportFund) {
		this.httpTransportFund = httpTransportFund;
	}
}
